package SiTuQingYou.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import SiTuQingYou.dao.CommentDao;
import SiTuQingYou.dao.LikeCollectDao;
import SiTuQingYou.util.Pager;
import SiTuQingYou.vo.Comment;
import SiTuQingYou.vo.User;

/**
 * Servlet implementation class MyCommentServlet
 */
@WebServlet("/MyCommentServlet")
public class MyCommentServlet extends BaseAction {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyCommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    //根据攻略id查询所有的评论
  protected void findAllCommentByStrategyId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  String id=request.getParameter("id");
	  int sid=Integer.parseInt(id); 
	  ArrayList<Comment> allComment = CommentDao.getcd().getallcomment(sid);
	  request.setAttribute("allComment",allComment);
	}
    
    
    
    //获得评论内容并存入数据库
    protected void saveComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String text=request.getParameter("text");
    	String id=request.getParameter("id");
        int sid=Integer.parseInt(id);
    	HttpSession session = request.getSession();
    	User user = (User)session.getAttribute("user"); 
    	PrintWriter out=response.getWriter();
    	if(user==null)
    	{
    		 
    		out.print("0");
    	}
    	else
    	{
     
    	   out.print(user.getUsernickname());
    	   CommentDao.getcd().addComment(user.getId(), sid, text);
    	}
    	
        
	}
    
    //根据用户id查找出我的所有评论
    protected void lookForAllComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	HttpSession session = request.getSession();
    	User user = (User)session.getAttribute("user");//得到登录的User对象
    	
    	Pager pager = Pager.init(request);//访问分页封装
       
      
    	
    	ArrayList<Comment> allComment = CommentDao.getcd().lookForAllMyComment(user.getId(),pager);
    	request.setAttribute("allComment",allComment);
	}
    
    //删除选中的评论
    protected void deleteAllChecked(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	String cid[] = request.getParameterValues("cid");
    	CommentDao.getcd().deleteAllCheckedComment(cid);
    	
    	//重新查询数量，并且传给jsp页面
    	HttpSession session = request.getSession();
    	User user = (User)session.getAttribute("user");
    	int count = CommentDao.getcd().getCommentNumber(user.getId());
    	PrintWriter out = response.getWriter();
  	    out.print(count);
	}
    
    
    @Override
	protected void doDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		
	}

}
